'use strict';

const Controller = require('egg').Controller;

class QuestionController extends Controller {
  // 获取题目列表
  async index() {
    const { ctx } = this;
    const { category_id, page = 1, pageSize = 5 } = ctx.query;
    try {
      const result = await ctx.service.question.findAll({ 
        category_id: category_id ? parseInt(category_id) : undefined,
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });
      ctx.body = {
        code: 200,
        message: '获取题目列表成功',
        data: result.data,
        pagination: result.pagination,
      };
    } catch (error) {
      ctx.logger.error('获取题目列表失败:', error);
      ctx.body = {
        code: 500,
        message: '获取题目列表失败',
        data: null,
      };
    }
  }

  // 根据ID获取题目
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    try {
      const question = await ctx.service.question.findById(id);
      if (question) {
        ctx.body = {
          code: 200,
          message: '获取题目详情成功',
          data: question,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '题目不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('获取题目详情失败:', error);
      ctx.body = {
        code: 500,
        message: '获取题目详情失败',
        data: null,
      };
    }
  }

  // 创建题目
  async create() {
    const { ctx } = this;
    const { category_id, question_content } = ctx.request.body;
    try {
      if (!category_id || !question_content) {
        ctx.body = {
          code: 400,
          message: '分类ID和题目内容不能为空',
          data: null,
        };
        return;
      }
      const question = await ctx.service.question.create({ category_id, question_content });
      ctx.status = 201;
      ctx.body = {
        code: 201,
        message: '创建题目成功',
        data: question,
      };
    } catch (error) {
      ctx.logger.error('创建题目失败:', error);
      ctx.body = {
        code: 500,
        message: '创建题目失败',
        data: null,
      };
    }
  }

  // 更新题目
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { category_id, question_content } = ctx.request.body;
    try {
      if (!question_content) {
        ctx.body = {
          code: 400,
          message: '题目内容不能为空',
          data: null,
        };
        return;
      }
      const updateData = { question_content };
      if (category_id) {
        updateData.category_id = category_id;
      }
      const question = await ctx.service.question.update(id, updateData);
      if (question) {
        ctx.body = {
          code: 200,
          message: '更新题目成功',
          data: question,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '题目不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('更新题目失败:', error);
      ctx.body = {
        code: 500,
        message: '更新题目失败',
        data: null,
      };
    }
  }

  // 删除题目
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;
    try {
      const result = await ctx.service.question.delete(id);
      if (result) {
        ctx.body = {
          code: 200,
          message: '删除题目成功',
          data: null,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '题目不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('删除题目失败:', error);
      ctx.body = {
        code: 500,
        message: '删除题目失败',
        data: null,
      };
    }
  }
}

module.exports = QuestionController;
